package com.nettest.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.nettest.entity.system.Staff;
import com.nettest.service.system.MenuService;
import com.nettest.service.system.StaffService;
import com.nettest.util.StringUtils;
@Service
public class BaseVerifyService {
	@Autowired
    private StaffService staffService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private PasswordEncoder passwordEncoder;

    /**
     * 判断用户账号是否已被占用
     * @param staffNo
     * @return
    */
    public boolean isStaffNoUsed(String staffNo) {
        Staff oldStaff = staffService.getOneStaff(staffNo);
        if (oldStaff != null) {
            return true;
        }
        return false;
    }

    /**
     * 判断传入的密码是否与数据库一致
     * @param staffNo
     * @param password
     * @return
    */
    public boolean isPasswordMatch(String staffNo, String password) {
        Staff oldStaff = staffService.getOneStaff(staffNo);
                
        if (oldStaff != null ) {
        	String pass1 = oldStaff.getPassword();
        	Object salt = oldStaff.getStaffNo();
            String pass2 = passwordEncoder.encodePassword(password, salt);
            return pass1.equals(pass2);
        }
        return false;
    }

    /**
     * 判断角色（包含子角色）是否已被用户关联
     * @param roleId
     * @return 已关联此角色的用户账号，多个以;分隔
    */
    public String isRoleRefByStaff(String roleId) {
        List<String> staffNos = staffService.getStaffNoByRoleId(roleId, true);
        return StringUtils.toStr(staffNos, ";");
    }
    /**
     * 判断菜单（包含子菜单）是否已被角色关联
     * @param roleId
     * @return 已关联此菜单的角色，多个以;分隔
     */
    public String isMenuRefByRole(String menuId) {
    	List<String> roleNames = menuService.getRoleNameByMenuId(menuId);
    	return StringUtils.toStr(roleNames, ";");
    }

    /**
     * 判断部门（包含子部门）是否已被用户关联
     * @param deptId
     * @return 已关联此部门的用户账号，多个以;分隔
    */
    public String isDeptRefByStaff(String deptId) {
        List<String> staffNos = staffService.getStaffNoByDeptId(deptId, true);
        return StringUtils.toStr(staffNos, ";");
    }
}
